package com.steven.download.download.db;

import android.os.Parcel;
import android.os.Parcelable;

import com.steven.download.AppConstant;
import com.steven.download.DownloadStatus;

import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey;

@Entity(tableName = "download" , indices = {@Index(value = {"url","threadId"},unique = true)})
public class DownloadEntity implements Parcelable {
    @PrimaryKey(autoGenerate = true)
    public int uid;
    private long start;
    private long end;
    private String url;
    private int threadId;
    private long progress;
    private long contentLength;

    private int status;
    private String localPath;


    private String id;
    private String createAt;
    private String updateAt;
    private String idApp;
    private String packageName;
    private String packageSize;
    private String packageFileSize;
    private String packageMd5;
    private boolean available;
    private String packageUrl;
    public DownloadStatus downloadStatus = DownloadStatus.IDLE;


    public DownloadEntity(long start, long end, String packageUrl, int threadId, long progress, long contentLength) {
        this.start = start;
        this.end = end;
        this.packageUrl = packageUrl;
        this.threadId = threadId;
        this.progress = progress;
        this.contentLength = contentLength;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public long getStart() {
        return start;
    }

    public void setStart(long start) {
        this.start = start;
    }

    public long getEnd() {
        return end;
    }

    public void setEnd(long end) {
        this.end = end;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public int getThreadId() {
        return threadId;
    }

    public void setThreadId(int threadId) {
        this.threadId = threadId;
    }

    public long getProgress() {
        return progress;
    }

    public void setProgress(long progress) {
        this.progress = progress;
    }

    public long getContentLength() {
        return contentLength;
    }

    public void setContentLength(long contentLength) {
        this.contentLength = contentLength;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getLocalPath() {
        return localPath;
    }

    public void setLocalPath(String localPath) {
        this.localPath = localPath;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCreateAt() {
        return createAt;
    }

    public void setCreateAt(String createAt) {
        this.createAt = createAt;
    }

    public String getUpdateAt() {
        return updateAt;
    }

    public void setUpdateAt(String updateAt) {
        this.updateAt = updateAt;
    }

    public String getIdApp() {
        return idApp;
    }

    public void setIdApp(String idApp) {
        this.idApp = idApp;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public String getPackageSize() {
        return packageSize;
    }

    public void setPackageSize(String packageSize) {
        this.packageSize = packageSize;
    }

    public String getPackageFileSize() {
        return packageFileSize;
    }

    public void setPackageFileSize(String packageFileSize) {
        this.packageFileSize = packageFileSize;
    }

    public String getPackageMd5() {
        return packageMd5;
    }

    public void setPackageMd5(String packageMd5) {
        this.packageMd5 = packageMd5;
    }

    public boolean isAvailable() {
        return available;
    }

    public void setAvailable(boolean available) {
        this.available = available;
    }

    public String getPackageUrl() {
        return packageUrl;
    }

    public void setPackageUrl(String packageUrl) {
        this.packageUrl = packageUrl;
    }

    public DownloadStatus getDownloadStatus() {
        return downloadStatus == null ? DownloadStatus.IDLE : downloadStatus;
    }

    public void setDownloadStatus(DownloadStatus downloadStatus) {
        this.downloadStatus = downloadStatus;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.uid);
        dest.writeLong(this.start);
        dest.writeLong(this.end);
        dest.writeString(this.url);
        dest.writeInt(this.threadId);
        dest.writeLong(this.progress);
        dest.writeLong(this.contentLength);
        dest.writeInt(this.status);
        dest.writeString(this.localPath);
        dest.writeString(this.id);
        dest.writeString(this.createAt);
        dest.writeString(this.updateAt);
        dest.writeString(this.idApp);
        dest.writeString(this.packageName);
        dest.writeString(this.packageSize);
        dest.writeString(this.packageFileSize);
        dest.writeString(this.packageMd5);
        dest.writeByte(this.available ? (byte) 1 : (byte) 0);
        dest.writeString(this.packageUrl);
        dest.writeInt(this.downloadStatus == null ? -1 : this.downloadStatus.ordinal());
    }

    public void readFromParcel(Parcel source) {
        this.uid = source.readInt();
        this.start = source.readLong();
        this.end = source.readLong();
        this.url = source.readString();
        this.threadId = source.readInt();
        this.progress = source.readLong();
        this.contentLength = source.readLong();
        this.status = source.readInt();
        this.localPath = source.readString();
        this.id = source.readString();
        this.createAt = source.readString();
        this.updateAt = source.readString();
        this.idApp = source.readString();
        this.packageName = source.readString();
        this.packageSize = source.readString();
        this.packageFileSize = source.readString();
        this.packageMd5 = source.readString();
        this.available = source.readByte() != 0;
        this.packageUrl = source.readString();
        int tmpDownloadStatus = source.readInt();
        this.downloadStatus = tmpDownloadStatus == -1 ? null : DownloadStatus.values()[tmpDownloadStatus];
    }

    protected DownloadEntity(Parcel in) {
        this.uid = in.readInt();
        this.start = in.readLong();
        this.end = in.readLong();
        this.url = in.readString();
        this.threadId = in.readInt();
        this.progress = in.readLong();
        this.contentLength = in.readLong();
        this.status = in.readInt();
        this.localPath = in.readString();
        this.id = in.readString();
        this.createAt = in.readString();
        this.updateAt = in.readString();
        this.idApp = in.readString();
        this.packageName = in.readString();
        this.packageSize = in.readString();
        this.packageFileSize = in.readString();
        this.packageMd5 = in.readString();
        this.available = in.readByte() != 0;
        this.packageUrl = in.readString();
        int tmpDownloadStatus = in.readInt();
        this.downloadStatus = tmpDownloadStatus == -1 ? null : DownloadStatus.values()[tmpDownloadStatus];
    }

    public static final Creator<DownloadEntity> CREATOR = new Creator<DownloadEntity>() {
        @Override
        public DownloadEntity createFromParcel(Parcel source) {
            return new DownloadEntity(source);
        }

        @Override
        public DownloadEntity[] newArray(int size) {
            return new DownloadEntity[size];
        }
    };
}
